//
//  TSSplicingTemplateListViewController.m
//  QHCollectionDemo
//
//  Created by MOiMac on 2021/3/8.
//

#import "TSSplicingTemplateListViewController.h"

#import "QHLayout.h"
#import "SplicingTemplateCollectionViewCell.h"


#import "TSSplicingTemplateEditViewController.h"
#import "XCPresentationScaleAnimation.h"
#import "XCPresentation.h"

#define SCREEN_W [UIScreen mainScreen].bounds.size.width


static NSString  * const cellID = @"cellid";

@interface TSSplicingTemplateListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *lineCollectionView;

@end

@implementation TSSplicingTemplateListViewController

- (instancetype)initWithImages:(NSArray<UIImage *> *)images {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _originImages = images;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = NSLocalizedString(@"选择模板(QHLayout)", nil);
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self.view addSubview:self.lineCollectionView];
    [self.lineCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.mas_topLayoutGuide);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
    }];
}

#pragma mark - collection delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger imageCount = self.originImages.count;
    if (imageCount == 2) {
        return 6;
    } else if (imageCount == 3) {
        return 6;
    } else if (imageCount == 4) {
        return 6;
    } else if (imageCount == 5) {
        return 6;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SplicingTemplateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    NSArray<UIImage *> *images = self.originImages;
    NSInteger styleIndex = indexPath.item+1;
    [cell updateImages:images styleIndex:styleIndex];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SplicingTemplateCollectionViewCell *cell = (SplicingTemplateCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    

    UIView *animationView = cell.templateContentView;
    CGRect rect = [animationView.superview convertRect:animationView.frame toView:nil];
    
    XCPresentationScaleAnimation *animation = [[XCPresentationScaleAnimation alloc] init];
    animation.animationView = animationView;
    animation.sourceFrame   = rect;
    animation.destFrame     = CGRectMake(16, 104, 382, 718);
    animation.duration = 2;

    NSArray<UIImage *> *images = self.originImages;
    NSInteger styleIndex = indexPath.item+1;
    TSSplicingTemplateEditViewController *viewController = [[TSSplicingTemplateEditViewController alloc] initWithImages:images styleIndex:styleIndex];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [XCPresentation presentWithPresentationAnimation:animation presentedViewController:navigationController presentingViewController:self];
}

#pragma mark - lazy
- (UICollectionView *)lineCollectionView{
    if (_lineCollectionView == nil) {
        QHLayout *layout = [[QHLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(75, SCREEN_W/2 - 200/2, 75, SCREEN_W/2 - 200/2);
        _lineCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _lineCollectionView.dataSource = self;
        _lineCollectionView.delegate = self;
        [_lineCollectionView registerClass:[SplicingTemplateCollectionViewCell class] forCellWithReuseIdentifier:cellID];
    }
    return _lineCollectionView;
}

@end
